package com.ciban.smarthome.module.ui.adapter;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.entity.Scene;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class SceneGridAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Scene> list;

    private boolean isModify = false;

    public SceneGridAdapter(Context context, ArrayList<Scene> list) {
        this.context = context;
        this.list = list;
    }

    public void setModify(boolean isModify) {
        this.isModify = isModify;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,
                    R.layout.griditem_common_content, null);
            holder = new ViewHolder();
            holder.iv_icon = (ImageView) convertView
                    .findViewById(R.id.item_gv_content_iv_icon);
            holder.tv_title = (TextView) convertView
                    .findViewById(R.id.item_gv_content_tv_title);
            holder.cb_sel = (CheckBox) convertView
                    .findViewById(R.id.item_gv_content_sel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Scene info = list.get(position);

        if (!StringUtils.isEmpty(info.getIcon_path()))
            holder.iv_icon.setImageBitmap(ImgUtils.getImageFromAssetsFile(context,
                    info.getIcon_path()));
        holder.tv_title.setText(info.getName());

        if (isModify) {
            holder.cb_sel.setVisibility(View.VISIBLE);
            if (info.isSel()) {
                holder.cb_sel.setChecked(true);
            } else {
                holder.cb_sel.setChecked(false);
            }
        } else {
            holder.cb_sel.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView iv_icon;
        TextView tv_title;
        CheckBox cb_sel;
    }

}
